package edu.yonsei.iwb.sdt.ontology;

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.ReasonerRegistry;
import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasonerFactory;
//import com.hp.hpl.jena.sdb.SDBFactory;
//import com.hp.hpl.jena.sdb.Store;
//import com.hp.hpl.jena.sdb.StoreDesc;
//import com.hp.hpl.jena.sdb.sql.JDBC;
//import com.hp.hpl.jena.sdb.sql.SDBConnection;
//import com.hp.hpl.jena.sdb.store.DatabaseType;
//import com.hp.hpl.jena.sdb.store.LayoutType;
import com.hp.hpl.jena.tdb.TDBFactory;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
import java.io.InputStream;



/**
 *
 * @author JDK
 */
public class OntologyModel {

    private Model model;
//    private OntModel model;
//    private Store store;    
    private int ontType = JenaModelType.MEMORY_MODEL;

    public OntologyModel(int type) {
        this.ontType = type;
        if (type == JenaModelType.MEMORY_MODEL) {            
            this.model = ModelFactory.createDefaultModel();
//            InputStream in = FileManager.get().open(fileName);
//            model.read(in, null);                    
        } else if (type == JenaModelType.TDB_MODEL) {
            this.model = TDBFactory.createModel(JenaModelType.MODEL_DIR);
        } else {
//            StoreDesc storeDesc = new StoreDesc(LayoutType.LayoutTripleNodesHash, DatabaseType.Derby);
//            JDBC.loadDriverDerby();
//            SDBConnection conn = new SDBConnection(JenaModelType.DB_URL, JenaModelType.DB_USER, JenaModelType.DB_PASSWORD);
//            this.store = SDBFactory.connectStore(conn, storeDesc);
//            this.model = SDBFactory.connectDefaultModel(store);
        }
    }

    public void loadOntology(String fileName) {
        if (this.ontType == JenaModelType.SDB_MODEL) {
//            this.store.getTableFormatter().create();
//            this.store.getTableFormatter().truncate();
        } else if(this.ontType == JenaModelType.TDB_MODEL) {
            this.model.removeAll();
        }
        InputStream in = FileManager.get().open(fileName);
        this.model.read(in, null);
        Resource configuration =  this.model.createResource();
        configuration.addProperty(ReasonerVocabulary.PROPruleMode, "forward");
        configuration.addProperty(ReasonerVocabulary.PROPruleSet,  "resources/rules/sdt.rules");
//        configuration.addProperty(ReasonerVocabulary.PROPruleSet,  "resources/rules/3.rules");
        Reasoner reasoner = GenericRuleReasonerFactory.theInstance().create(configuration);
        InfModel infmodel = ModelFactory.createInfModel(reasoner, this.model);
        this.model.add(infmodel.getDeductionsModel());
//        Reasoner rdfsReasoner = ReasonerRegistry.getRDFSReasoner();
//        InfModel rdfsInfmodel = ModelFactory.createInfModel(rdfsReasoner, model);
//        this.model.add(rdfsInfmodel);
        
        if (this.ontType == JenaModelType.TDB_MODEL) {
            model.close();
        }
    }

    public Model getJenaModel() {
        if (this.ontType == JenaModelType.TDB_MODEL) {
            this.model = TDBFactory.createModel(JenaModelType.MODEL_DIR);
        }
        return this.model;
    }
    
    

}
